package me.cxlr.qinlauncher2.dao;

import androidx.annotation.NonNull;

import java.util.Objects;

import io.realm.DynamicRealm;
import io.realm.RealmMigration;
import io.realm.RealmObjectSchema;
import io.realm.RealmSchema;
import me.cxlr.qinlauncher2.model.AppIcon;

/**
 * 多帐本管理的数据库迁移
 *
 * @author 酷安用户:山轮纸
 */
public class DataBaseMigration implements RealmMigration {

    @Override
    public void migrate(@NonNull final DynamicRealm realm, long oldVersion, long newVersion) {
        RealmSchema schema = realm.getSchema();

        // v1->v2 AppIcon表添加字段
        if (oldVersion == 1) {
            RealmObjectSchema realmObjectSchemaAppIcon = Objects.requireNonNull(schema.get("AppIcon"))
                    .addField("componentName", String.class);

            Objects.requireNonNull(schema.get("App"))
                    .addField("useCustomAppIcon", boolean.class)
                    .addRealmObjectField("appIcon", realmObjectSchemaAppIcon)
                    .transform(obj -> {
                        obj.setBoolean("useCustomAppIcon", false);
                        obj.setObject("appIcon", null);
                    });
            oldVersion += 1;
        }
    }
}
